1-2 uW銧周簡d

當你下載 Perl 直譯器並安裝於你的電腦後,Perl 應該就已經位於你的電腦的執行路徑之中了,因此要測試 Perl 是否安裝完成,只需在命令視窗下輸入: perl -v 此時電腦螢幕即會回應類似下列之訊息: This is perl, v5.6.0 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2000, Larry Wall Binary build 613 provided by ActiveState Tool Corp. http://www.ActiveState.com Built 12:36:25 Mar 24 2000 Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5.0 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using `man perl' or `perldoc perl'. If you have access to the Internet, point your browser at http://www.perl.com/, the Perl Home Page. 若你見到此訊息,大概就可以確認 Perl 已安裝完成。(由此訊息也可以看出,我的 Perl 是由 ActiveState 公司修改後的 Win32 版本。)

若要取得精簡的線上支援,可以輸入:

perl -h 回應訊息如下: Usage: C:\Perl\bin\Perl.exe [switches] [--] [programfile] [arguments] -0[octal] specify record separator (\0, if no argument) -a autosplit mode with -n or -p (splits $_ into @F) -C enable native wide character system interfaces -c check syntax only (runs BEGIN and END blocks) -d[:debugger] run program under debugger -D[number/list] set debugging flags (argument is a bit mask or alphabets) -e 'command' one line of program (several -e's allowed, omit programfile) -F/pattern/ split() pattern for -a switch (//'s are optional) -i[extension] edit <> files in place (makes backup if extension supplied) -Idirectory specify @INC/#include directory (several -I's allowed) -l[octal] enable line ending processing, specifies line terminator -[mM][-]module execute `use/no module...' before executing program -n assume 'while (<>) { ... }' loop around program -p assume loop like -n but print line also, like sed -P run program through C preprocessor before compilation -s enable rudimentary parsing for switches after programfile -S look for programfile using PATH environment variable -T enable tainting checks -u dump core after parsing program -U allow unsafe operations -v print version, subversion (includes VERY IMPORTANT perl info) -V[:variable] print configuration summary (or a single Config.pm variable) -w enable many useful warnings (RECOMMENDED) -W enable all warnings -X disable all warnings -x[directory] strip off text before #!perl line and perhaps cd to directory 這些精簡的線上支援,對初學者來說,可能還是深奧了一些。若要取得更詳細的線上支援,可以嘗試 perldoc 命令,例如: perldoc perl 若要查詢 perldoc 本身的功能,可輸入: perldoc perldoc 若要查詢 perl 的各種函數,可以輸入: perldoc perlfunc 若要查詢某一個特定的函數,例如 print,可輸入如下: perldoc -f print 由於上述命令回應的訊息相當多,在此不一一列出,讀者可自行試試這些命令。

Perl 也有 HTML 格式的線上支援,假設你的 Perl 的安裝目錄是 c:\perl,那麼你可以在 DOS 命令視窗下輸入下列敘述,以 Web 瀏覽器開啟 HTML 格式的線上支援:

start c:\perl\html\index.html 所開啟的線上支援如下:

我們先看一個最簡單的 Perl 程式,其功能只是印出一列文字「Hello World!」,程式碼(hello.pl)如下:

原始檔(hello.pl):(灰色區域按兩下即可拷貝)
#! /user/local/bin/perl
# My first Perl program
print "Hello World!";

假設包含上述程式碼的檔案為 hello.pl,若要執行此檔案,可在 DOS 命令視窗下輸入下列文字:

perl hello.pl 此時即會在銀幕上印出「Hello World!」。我們現在逐列解釋程式碼,如下:
  1. 只要是以「#」開頭的敘述,就代表是註解,對程式的執行完全無影響。因此第一列程式碼在 Win32 平台完全不起作用。但在 UNIX 平台,以「#!」開頭的敘述,即告訴作業系統應如何執行此程式檔,以此例而言,作業系統會呼叫「/usr/local/bin/perl」來讀入此程式檔並執行之。換句話說,在 UNIX 平台上,除了可以使用上述的方法來執行 hello.pl 之外,亦可由下列方式將 hello.pl 改成可執行檔: chmod +x hello.pl chmod 755 hello.pl 然後在 UNIX 命令視窗下直接輸入「hello.pl」,此時作業系統就會經由包含「#!」的第一列,找出 Perl 的路徑,並由此 Perl 來執行每列程式碼。
  2. 第二列程式碼是一個標準的註解,用來說明此程式的功能。
  3. 第三列則是利用 print 指令來印出「Hello World!」。
在 Win32 平台上,是否能夠直接輸入 Perl 程式檔,即可開始執行?答案是肯定的。在NT4.0以上的版本,你只要在 DOS 命令視窗輸入下列敘述即可: assoc .pl=perl ftype perl=c:\perl\bin\perl.exe %1 %* (其中至第二列敘述中,你必須完整的寫出 Perl 的路徑。)執行完這些敘述後,就可以在 DOS 命令視窗下輸入「hello.pl」來執行此程式碼。此外,你也可以直接輸入「hello」來執行 hello.pl,但必須先在 DOS 命令視窗下輸入下列敘述: set PATHEXT=%PATHEXT%;.pl 在上述的說明中,我們都假設 Perl 程式碼在 Win32 平台的副檔名是 pl,但這只是一般傳統而已,你也可以選用其他的副檔名,以符合個人喜好。(但若選用不同的副檔名,在上述的命令中,你就必須將 pl 改成新的副檔名。)

在本章其後的說明和範例,都是以 ActiveState 公司所修改的 Win32 Perl 版本為主。若你使用的 Perl 版本和本書不同,結果可能略有不同,但應該大同小異。


Perl